package com.kedacom.demo.modb.weibo.handler;

import javax.annotation.Resource;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.kedacom.demo.modb.rabbitmq.message.CreateAccountEventMessage;
import com.kedacom.demo.modb.rabbitmq.message.ModbMessage;
import com.kedacom.demo.modb.weibo.dao.APAccountDao;
import com.kedacom.demo.modb.weibo.dao.TsoAccountDao;
import com.kedacom.demo.modb.weibo.model.Account;

/**
 * {@link ModbMessage}消息处理类
 * @author luocanfeng
 * @date 2013-1-11
 */
@Transactional
@Component("modbMessageHandler")
public class ModbMessageHandler {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Resource
	private TsoAccountDao tsoAccountDao;
	@Resource
	private APAccountDao apAccountDao;

	public void handleMessage(CreateAccountEventMessage message) {
		logger.debug("Received: {}", ToStringBuilder.reflectionToString(message, ToStringStyle.SHORT_PREFIX_STYLE));

		String moid = message.getAccountMoid();
		Account account = apAccountDao.getByMoid(moid);
		logger.debug("Fetched account from apdb: {}",
				ToStringBuilder.reflectionToString(account, ToStringStyle.SHORT_PREFIX_STYLE));

		tsoAccountDao.copy(account);
		logger.debug("Coped account to tsodb.");
	}

}
